﻿@{
    var tableName = PageViewModel.PageTableID(this);
    var tableToolBar = PageViewModel.PageTableToolbar(this);
    var tableToolBarSearch = PageViewModel.PageTableToolbarSearch(this);
    var tableToolBarReset = PageViewModel.PageTableToolbarReset(this);

    var editDepartmentDlgName = PageViewModel.PageEditDialog(this);
    var editDepartmentFunctionName = PageViewModel.PageEditDialog(this, "edit_dept", "js_fun");
    var saveDepartmentFunctionName = PageViewModel.PageFunctionName(this, "save_edit_dept");
    var deleteDepartmentFunctionName = PageViewModel.PageFunctionName(this, "delete_dept");
}


<script type="text/javascript">
    function @editDepartmentFunctionName (id) {
        var dlg = $dlg('@editDepartmentDlgName');
        dlg.dialog({
            title: '部门信息', width: 950, height: 550,
            closed: false, cache: false, modal: true, maximizable: false, resizable: true,
            href: '~/Organization/_EditDepartment?NOT_ID=' + id,
            onResize: function () {
                $(this).dialog('center');
            },
            buttons: [
                {
                    text: "保 存",
                    handler: function () { @saveDepartmentFunctionName (); },
                    iconCls: "icon-save"
                }, {
                    text: "取 消",
                    handler: function () { $("#@editDepartmentDlgName").dialog("close"); },
                    iconCls: "icon-cancel"
                }
            ],
            onLoad: function () {
                LoadObject(id, "~/Departments/GetDepartmentByID/", function (dept) {
                    var form = $("#@editDepartmentDlgName").find("form");
                    if (dept.ParentID == 0) { dept.ParentID = dept.OrgID * 1000000; }
                    form.form("load", dept);
                    if (dept.Enable) {
                        form.find("#Enable").switchbutton("check");
                    } else {
                        form.find("#Enable").switchbutton("uncheck");
                    }
                });
            }
        });
    }
    
    function @saveDepartmentFunctionName () {
        var form = $("#@editDepartmentDlgName").find("form");
        if (form.form('enableValidation').form('validate')) {
            var dept = form.form_to_json(function (form, dept) {
                dept.Enable = form.find("#Enable").switchbutton("options").checked;
                var node = form.find("#ParentID").combotree("tree").tree("getSelected");
                if (node != null) {
                    dept.OrgID = node.OrgID;
                    if (node.NodeType == "ORG") {
                        dept.ParentID = 0;
                    }
                }
            });
    
            $.ajax({
                url: "~/Departments/SaveDepartment",
                type: "POST",
                data: dept,
                success: function (e) {
                    if (e.Result) {
                        $.messager.alert('保存成功', '保存成功!', 'info', function () {
                            $('#@editDepartmentDlgName').dialog('close');
                            $("#@tableName").treegrid("reload");
                        });
                    } else {
                        $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e) {
                    ShowAjaxError(e);
                }
            });
        }
    }
    
    function @deleteDepartmentFunctionName (id) {
        if (id > 0 && confirm("确认删除部门信息?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Departments/DeleteDepartment/" + id),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        this.treegrid('reload');
                    } else {
                        $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据错误");
                    this.treegrid('reload');
                }
            });
        }
    }
</script>

<script type="text/javascript">
    function @tableToolBarSearch {
        $('#@tableName').treegrid('load', {
            DepartmentName: $("#@tableToolBar").find("#DepartmentName").textbox("getValue")
        });
    }

    function @tableToolBarReset {
        $("#@tableToolBar").find("#DepartmentName").textbox("setValue", "");
        @tableToolBarSearch;
    }
</script>

<table id="@tableName" class="easyui-treegrid" style="width:100%;height:100%" rownumbers="true" fit="true" fitcolumns="false"
       pagination="true" pageSize="@SystemSettingService.Instance.PageSize" pageList="@SystemSettingService.Instance.PageList"
       data-options="striped:true,border:false,singleSelect:true,url:'~/Departments/GetDepartments',method:'get',idField: 'Id',treeField: 'FullName',toolbar:'#@tableToolBar'">
    <thead>
        <tr>
            <th data-options="field:'_edit',align:'center',formatter:function(v,r,i){
                    return GridRowEditFmt('@editDepartmentFunctionName', r.Id);
                }" width="45">修改</th>
            <th data-options="field:'_remove',align:'center',formatter:function(v,r,i){
                    return GridRowDeleteFmt('@deleteDepartmentFunctionName', r.Id);
                }" width="45">删除</th>
            <th data-options="field:'Code',width:80">编码</th>
            <th data-options="field:'FullName',width:160">全称</th>
            <th data-options="field:'ShortName',width:140">简称</th>
            <th data-options="field:'DepartmentType',width:100,formatter:function(v,r,i){
                    var u = GetUrlObject('~/Systems/GetAppDictionaryByTID?typeCode=DEPARTMENT_TYPE&id='+v);
                    if(u != null) { return u.Name; }
                }">部门类型</th>
            <th data-options="field:'Address',width:140">地址</th>
            <th data-options="field:'PhoneNumber',width:100">电话</th>
            <th data-options="field:'OrgID',width:140,formatter:function(v,r,i){
                    var u = GetObject(v, '~/Organization/GetOrganizationByID');
                    if(u != null) { return u.FullName; }
                }">机构</th>
            <th data-options="field:'Email',width:120">邮件</th>
            <th data-options="field:'PostCode',width:80">邮编</th>
            <th data-options="field:'Enable',width:80,align:'center',formatter:$.CheckdFormat">是否启用</th>
            <th data-options="field:'Longitude',width:100,align:'center',
                            formatter:function(v,r,i){
                                if(r.Longitude&gt;0 && r.Latitude&gt;0) {
                                    return '('+r.Longitude+'，'+r.Latitude+')';
                                }
                            }">经维度</th>
        </tr>
    </thead>
</table>
<div id="@tableToolBar" class="table-toolbar">
    <a href="javascript:@editDepartmentFunctionName ()" class="easyui-linkbutton t-btn"><i class="fa fa-plus-circle icon" style="color:green"></i><span>新增</span></a>
    <ul class="query-body">
        <li>
            <span>部门名称：</span>
            <input id="DepartmentName" class="easyui-textbox" data-options="prompt:'部门名称'" />
        </li>
        <li>
            <a href="javascript:@tableToolBarSearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
            <a href="javascript:@tableToolBarReset" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">重置</a>
        </li>
    </ul>
</div>